package com.lb.activity.service.impl;

import com.lb.activity.mapper.UserGroupMapper;
import com.lb.activity.mapper.UserMapper;
import com.lb.activity.service.UserService;
import com.lb.activity.vo.User;
import com.lb.activity.vo.UserExample;
import com.lb.activity.vo.UserGroup;
import com.lb.activity.vo.UserGroupExample;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.identity.Group;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 001030
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserGroupMapper userGroupMapper;

    @Resource
    private IdentityService identityService;

    @Resource
    private ProcessEngine engine;

    @Override
    public List<User>isLogin(User user) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(0L);
        criteria.andUserNameEqualTo(user.getUserName());
        criteria.andPasswordEqualTo(user.getPassword());
        List<User> users = userMapper.selectByExample(example);
        return users;
    }

    /**
     * 无用代码
     */
    @Override
    public void bindMemberShip() {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(0L);
        UserGroupExample example1 = new UserGroupExample();
        List<UserGroup> userGroups = userGroupMapper.selectByExample(example1);
        for (UserGroup userGroup : userGroups) {
            Group group = engine.getIdentityService().newGroup(userGroup.getId() + "");
            group.setName(userGroup.getName());
            group.setType("assignment");
            identityService.saveGroup(group);
        }
        List<User> users = userMapper.selectByExample(example);
        for (User user : users) {
            org.activiti.engine.identity.User userAct = engine.getIdentityService().newUser(user.getId() + "");
            userAct.setPassword(user.getPassword());
            userAct.setFirstName(user.getUserName());
            identityService.saveUser(userAct);
        }

        for (User user : users) {
            identityService.createMembership(user.getId()+"",user.getGroupId()+"");
        }
    }

    @Override
    public User getAdminUserByLoginName(String username) {
        return userMapper.selectByUserLoginName(username);
    }

    @Override
    public List<User> getuserList(User userNew) {
        UserExample example = new  UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUserNameEqualTo(userNew.getUserName());
        criteria.andPasswordEqualTo(userNew.getPassword());
        return userMapper.selectByExample(example);
    }
}
